//
//  ShareDetailsVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/6.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class ShareDetailsVc: TTBaseVC {
    let ShareDetailsTableViewCellID = "ShareDetailsTableViewCell"
    
    var programID:Int = 0
    lazy var shareHeadModel = ShareModel()
    lazy var shareListModel = [ShareModel]()
    lazy var userModel = [UserInfoModel]()
    
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(ShareDetailsTableViewCellID), forCellReuseIdentifier: ShareDetailsTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 10))
        tableView.setNoData(nodata: true)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "分享详情"
        
        self.view.addSubview(tableView)
        SET_LINEVIEW(view: self.view)
        
        setHeadData()
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

//  MARK: - ###### 事件 ######
extension ShareDetailsVc {
    func setHeadData() {
        MTProgressHUD.show()
        WelfareRequestModel.loadShareDetailsHeadData(jointArr: [self.programID ,String.getUserId(),String.getUserId()]) { (success, message, head, user) in
            if success && head != nil {
                self.shareHeadModel = head!
                self.userModel = user!
                self.setListData()
            }
            MTProgressHUD.hidded(true)
        }
    }
    
    func setListData() {
        MTProgressHUD.show()
        WelfareRequestModel.loadProgramShareData(parameters: ["startPoint":1,"maxPage":999]) { (success, message, model) in
            if success && model != nil {
                self.shareListModel = model!
                self.tableView.setNoData(nodata: false)
                self.tableView.reloadData()
            }
            MTProgressHUD.hidded(true)
        }
    }
    
    func shareAction() {
        HomeRequestModel.loadResourcesData(parameters: ["flg":"get_share_detail"]) { (success, message, urlStr) in
            if success {
                let shareUrl = urlStr + "?programId=\(self.shareHeadModel.id)&srcUserId=\(String.getUserId())"
                let share = ShareView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), title: self.shareHeadModel.title ?? "天天必听", des: self.shareHeadModel.describe ?? "分享免费听", url: shareUrl, image: self.shareHeadModel.shareImageUrl ?? "http://source.blistening.cn/bt_source/ttbt/logo/logo.png")
                UIApplication.shared.keyWindow?.addSubview(share)
            }
        }
        
        HomeRequestModel.UpdateMyProgramSharePost(parameters: ["programId":self.shareHeadModel.id,"userId":String.getUserId()]) { (success, message) in
            
        }
    }
}

//  MARK: - ###### 协议 ######
extension ShareDetailsVc: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            if self.shareListModel.count > 0 {
                return 1
            } else {
                return 0
            }
        case 1:
            return 0
        case 2:
            return self.shareListModel.count
        default:
            return 0
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0:
            return 138
        case 1:
            return 0
        case 2:
            return 138
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 0
        case 1:
            return 240
        case 2:
            return 25
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 0:
            return UIView.init()
        case 1:
            if self.shareListModel.count > 0 {
                let view = ShareDetailsFriendView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 240))
                view.userModel = self.userModel
                view.setDate(shareTime: self.shareHeadModel.shareCreateTime / 1000, survivalTime: self.shareHeadModel.survivalTime / 1000)
                view.inviteBlock = { () in
                    self.shareAction()
                }
                return view
            } else {
                return UIView.init()
            }
        case 2:
            if self.shareListModel.count > 0 {
                let view = ShareDetailsHeaderView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 25))
                view.moreBlock = { () in
                    let vc = ShareFreeListenVc()
                    PUSHVIEWCONTROLLER(vc)
                }
                return view
            } else {
                return UIView.init()
            }
        default:
            return UIView.init()
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: ShareDetailsTableViewCellID) as! ShareDetailsTableViewCell
            cell.backgroundColor = UIColor.clear
            cell.selectionStyle = .none
            cell.titleLabel.text = self.shareHeadModel.title
            cell.leftImageView.sd_setImage(with: URL(string:(StringIncludeChineseToString(self.shareHeadModel.shareImageUrl ?? ""))), placeholderImage: IMAGE(name: "welfarePlace"), options: .lowPriority, completed: nil)
            cell.freeListenLabel.text = "\(self.shareHeadModel.mockTotalUser)人免费听"
            cell.detailsLabel.text = self.shareHeadModel.title
            cell.listenNumberLabel.text = "\(self.shareHeadModel.listenNum)"
            cell.tolLabel.text = "\(self.shareHeadModel.totalSeason)集"
            cell.priceLabel.text = "\(String.money(self.shareHeadModel.totalCount ?? 0.0) ?? "0")听币"
            return cell
        case 1:
            return UITableViewCell.init()
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: ShareDetailsTableViewCellID) as! ShareDetailsTableViewCell
            cell.backgroundColor = UIColor.clear
            cell.selectionStyle = .none
            let model = self.shareListModel[indexPath.row]
            cell.titleLabel.text = model.title
            cell.leftImageView.sd_setImage(with: URL(string:(StringIncludeChineseToString(model.shareImageUrl ?? ""))), placeholderImage: IMAGE(name: "welfarePlace"), options: .lowPriority, completed: nil)
            cell.freeListenLabel.text = "\(model.mockTotalUser)人免费听"
            cell.detailsLabel.text = model.title
            cell.listenNumberLabel.text = "\(model.listenNum)"
            cell.tolLabel.text = "\(model.totalSeason)集"
            cell.priceLabel.text = "\(String.money(model.totalCount ?? 0.0) ?? "0")听币"
            return cell
        default:
            return UITableViewCell.init()
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let model = self.listenModel[indexPath.row]
//        let playVc = PlayVC()
//        playVc.id = model.id
//        PUSHVIEWCONTROLLER(playVc)
        
    }
}
